﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SlimDX;
using SlimDX.Direct3D9;
using Application = ThreeDI.UI.Abstraction.Application;
using Mesh = ThreeDI.UI.Abstraction.Mesh;


namespace ThreeDI.UI
{
   public class MouseMesh:Mesh
    {
        
        private const string path
                = @".\Assets\arrow\arrow.x";
       private float x;
       private float y;  
       public Application _app;
       private SkyBox skyboxMesh;
       private SlimDX.Direct3D9.Mesh _Mesh;
       private SlimDX.Vector3 maxv;
       private SlimDX.Vector3 minv;
       private SlimDX.Vector3 center;
       private float _skyBoxWidth;
       private float _skyBoxHeight;
   //    private SlimDX.Vector3 X3d;
       private float _X3d;
       private float _Y3d; 
       //SlimDX.Vector3 prevX3d;
       private float prevX3d ;
       private float prevY3d ;
       //private SlimDX.Vector3 Y3d;
     //  private SlimDX.Vector3 prevY3d;
       private int resolutionX;
       private int resolutionY;
       private float ScreenX;
       private float ScreenY;
       private float prevScreenX ;
       private float prevScreenY ;
       private float _Xpos;
       private float _Ypos;
       private float _x;
       private float _y;
        public MouseMesh(Application application):base(path,application)
        {
            _app = application;
            _app.MouseMove += new System.Windows.Forms.MouseEventHandler(_app_MouseMove);
          
        }

       private int i = 0;
      
        void _app_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (i == 0)
            {
                _Xpos = e.Location.X;
                _Ypos = e.Location.Y;
                i++;
            }
            
                if (e.Location.X != _Xpos || e.Location.Y != _Ypos)
                {
                    if (prevX3d != Cursor.Position.X || prevY3d != Cursor.Position.Y)
                    {
                        //ScreenX = e.Location.X;
                        //ScreenY = e.Location.Y;
                        GetMousePosition();

                    }
                }
            
            //_Xpos = Cursor.Position.X;
            //_Ypos = Cursor.Position.Y;
           
        }

  
      
       public override void Load()
       {
           Cursor.Position = new Point((Cursor.Position.X - Cursor.Position.X) + _app.Width / 2, (Cursor.Position.Y - Cursor.Position.Y)+_app.Height/2);      
            base.Load();
            mousePositionSettings(out resolutionX, out resolutionY, out _skyBoxWidth, out _skyBoxHeight);
            //_Xpos = Cursor.Position.X;
            //_Ypos = Cursor.Position.Y;
       }

      
        public override void Render()
        { 
            base.Render();   
        }


        public void mousePositionSettings(out int resolutionX, out int resolutionY, out float _skyBoxWidth, out float _skyBoxHeight)
       {
         
           resolutionX = _app.Width;
            resolutionY = _app.Height;
            ComputeBoundingBox();
            maxv = _maxValue;
            minv = _minValue;
           _skyBoxWidth = maxv.X - minv.X;
            _skyBoxHeight = maxv.Y - minv.Y;
       }

        public void GetMousePosition()
        {
            ScreenX = System.Windows.Forms.Cursor.Position.X;
            int hah = System.Windows.Forms.Cursor.Position.X;
            _X3d = (ScreenX * _skyBoxWidth) / resolutionX;
            ScreenY = System.Windows.Forms.Cursor.Position.Y;
            _Y3d = (ScreenY * _skyBoxHeight) / resolutionY;
           
            _X3d -= prevX3d;
            _Y3d -= prevY3d;
            prevX3d = _X3d;
            prevY3d = -_Y3d;
            if (prevX3d != _X3d || prevY3d != _Y3d)
            {
                if(ScreenX==0 && ScreenY<0)
                {
                    Translation(0,-_Y3d,0);
                }
                if(ScreenX==0&&ScreenY>0)
                {
                    Translation(0, _Y3d, 0);
                }
                if (ScreenX > 0 && ScreenY == 0)
                {
                    Translation(-_X3d, 0, 0);
                }
                if (ScreenX <0 && ScreenY == 0)
                {
                    Translation(-_X3d, 0, 0);
                }

                if (ScreenX > 0 && ScreenY < 0)
                {
                    Translation(_X3d,- _Y3d, 0);
                }
              

                if (ScreenX < 0 && ScreenY >0)
                {
                    Translation(-_X3d, _Y3d, 0);
                }


                if (ScreenX > 0 && ScreenY > 0)
                {
                    Translation(_X3d, _Y3d, 0);
                }



                if (ScreenX < 0 && ScreenY < 0)
                {
                    Translation(-_X3d, -_Y3d, 0);
                }

            }
            //// ScreenX = Cursor.Position.X;
           // _X3d = (prevX3d * ScreenX) / prevScreenX;
           // prevScreenX = ScreenX;
           // prevX3d = _X3d;

           //// ScreenY = Cursor.Position.Y;
           // _Y3d = (prevY3d * ScreenY) / prevScreenY;
           // prevScreenY = ScreenY;
           // prevY3d = _Y3d;
           
            //if(prevX3d!=_X3d||prevY3d!=_Y3d)
            //{
            //    Translation(_X3d, _Y3d, 0);
            //}
       }


}
    }
